﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/4/7 星期日 20:17:11
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.Core;

namespace Sector7.Framework.DataDictionary.Agents
{
    /// <summary>
    /// 数据分组服务器代理类
    /// </summary>
    public class DataGroupServiceAgent
    {
        /// <summary>
        /// 添加数据分组
        /// </summary>
        /// <param name="group">数据分组</param>
        /// <returns>数据分组编号</returns>
        public static int Add(S7_DataGroup group)
        {
            object groupId = WcfClient.GetInstance().Invoke<object>(
                service => service.AddDataGroup(group));

            return Convert.ToInt32(groupId);
        }

        /// <summary>
        /// 修改数据分组
        /// </summary>
        /// <param name="delta">数据分组</param>
        /// <returns>操作结果</returns>
        public static int Set(EntityDeltaBase delta)
        {
            return WcfClient.GetInstance().Invoke<int>(
                service => service.SetDataGroup(delta));
        }

        /// <summary>
        /// 获取所有数据分组
        /// </summary>
        /// <returns>数据分组列表</returns>
        public static List<S7_DataGroup> GetAll()
        {
            List<S7_DataGroup> groups = null;

            try
            {
                groups = WcfClient.GetInstance().Invoke<List<S7_DataGroup>>(
                        service => service.GetAllDataGroups());
            }
            catch (Exception e)
            {
                Log.WriteLine(LogSeverityType.ERR, "DAD", e);
            }

            return groups;
        }

        /// <summary>
        /// 根据数据分组标识获取实例
        /// </summary>
        /// <param name="groupIdentifier">数据分组标识</param>
        /// <returns>数据分组</returns>
        public static S7_DataGroup Get(string groupIdentifier)
        {
            return WcfClient.GetInstance().Invoke<S7_DataGroup>(
                service => service.GetDataGroupByIdentifier(groupIdentifier));
        }

        /// <summary>
        /// 根据数据分组类型获取所有数据分组
        /// </summary>
        /// <param name="typeId">数据分组类型</param>
        /// <returns>数据分组列表</returns>
        public static List<S7_DataGroup> GetAll(int typeId)
        {
            List<S7_DataGroup> elements = null;

            try
            {
                elements = WcfClient.GetInstance().Invoke<List<S7_DataGroup>>(
                    service => service.GetAllDataGroupsByTypeId(typeId));
            }
            catch (Exception e)
            {
                Log.WriteLine(LogSeverityType.ERR, "DAD", "GetAllDataGroupsByTypeId 读取数据分组列表异常！");
                Log.WriteLine(LogSeverityType.ERR, "DAD", e);
            }

            return elements;
        }
    }
}
